#include <iostream>
#include <fstream>
#include <hunspell/hunspell.h>
#include <Judy.h>

bool isNonChar( const char* str, unsigned int len)
{
    const char* pos(str);
    const char* end(str + len);
    while ((pos != end) and ('a' <= *pos) and (*(pos++) <= 'z' )) {
    }

    return pos != end;
}

void formStopWordsList( Pvoid_t& pStopWords, const std::string& filePath )
{
    std::ifstream   swFile(filePath.c_str());
    std::string     stopWord("");
    Pvoid_t         defaultValue;
    while (std::getline(swFile, stopWord)) {
        JSLI(defaultValue, pStopWords, ((const uint8_t*)stopWord.c_str()));
    }
}


int main( int argc, char** argv )
{
    if (argc < 4) {
        std::cerr << argv[0] << " \"hunspell aff\" \"hunspell dic\" \"stopwords file\"" << std::endl;
        return 1;
    }



    Hunhandle*  pHunspell(Hunspell_create( argv[1]
                                         , argv[2] ));

    Pvoid_t     pStopWords(0);

    formStopWordsList(pStopWords, argv[3]);
                        
	std::string wordForStem("");

    while (std::getline(std::cin, wordForStem)) {

        if (isNonChar(wordForStem.c_str(), wordForStem.size())) {
            std::cout << "\tcontains not chars" << std::endl;
            continue;
        }

        Pvoid_t PValue(0);


        JSLG(PValue, pStopWords, (const uint8_t*)wordForStem.c_str());
        if (PValue != 0) {
            std::cout << "\tthis is stop word" << std::endl;
            continue;
        }
        
        char**  ppStemResults;
        int     resCount(Hunspell_stem( pHunspell
                                      , &ppStemResults
                                      , wordForStem.c_str() ));

        for (int i = 0; i != resCount; ++i) {
            std::cout << "\tstem : " << ppStemResults[i] << std::endl;
        }

        Hunspell_free_list(pHunspell, &ppStemResults, resCount);
        
    }

    Word_t  bytes;
    JSLFA(bytes, pStopWords);
    Hunspell_destroy(pHunspell);
} 
